package com.fywsy.fyweb.repository;

import com.fywsy.fyweb.entity.Game;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GameRepository extends JpaRepository<Game, Long> {

    List<Game> getAllByEnableAndVisableOrderByCreateTimeDesc(boolean enable, boolean visable);

    List<Game> getAllByEnableAndVisableAndExternalReadAndCateOrderByCreateTimeDesc(boolean enable, boolean visable, boolean read, int cate);

    List<Game> findAllByEnableAndVisableAndTagsContaining(boolean enable, boolean visable, String cate);

    Game findFirstByEnableAndName(boolean enable, String name);

    @Query("SELECT g FROM Game g WHERE g.enable = true AND g.visable = true AND " +
           "(LOWER(g.name) LIKE LOWER(CONCAT('%', :word, '%')) OR " +
           "LOWER(g.intro) LIKE LOWER(CONCAT('%', :word, '%')) OR " +
           "LOWER(g.tags) LIKE LOWER(CONCAT('%', :word, '%'))) " +
           "ORDER BY g.createTime DESC")
    List<Game> searchGames(@Param("word") String word);
}